package com.example.springboot.time;

import lombok.Getter;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * 留样餐次配置枚举
 */
@Getter
public enum BusiSampleMealTimeConfigEnum {

    ZC1("早餐",1,"早餐",1,LocalTime.of(4,0,0),LocalTime.of(6,59,59)),
    ZC2("早点",6,"早上点心",2,LocalTime.of(7,0,0),LocalTime.of(8,59,59)),
    WUC1("午餐",2,"午餐",3,LocalTime.of(9,0,0),LocalTime.of(12,59,59)),
    WUC2("午点",7,"下午茶",4,LocalTime.of(13,0,0),LocalTime.of(15,59,59)),
    WANC("晚餐",3,"晚餐",5,LocalTime.of(16,0,0),LocalTime.of(20,59,59)),
    YX("夜宵",4,"夜宵",6,LocalTime.of(21,0,0),LocalTime.of(2,59,59)),
    ;

    BusiSampleMealTimeConfigEnum(String name, Integer type, String otherName, int sort, LocalTime startTime, LocalTime endTime){
        this.name=name;
        this.type=type;
        this.otherName=otherName;
        this.sort=sort;
        this.startTime=startTime;
        this.endTime=endTime;
    }
    /**
     * 名称
     */
    private final String name;

    /**
     * 字段
     */
    private final Integer type;

    private final String otherName;

    private final int sort;

    private final LocalTime startTime;
    private final LocalTime endTime;

    private static final Map<Integer, BusiSampleMealTimeConfigEnum> enumMap=new HashMap<>();
    private static final Map<String, BusiSampleMealTimeConfigEnum> enumMealNameMap=new HashMap<>();

    static {
        Arrays.stream(BusiSampleMealTimeConfigEnum.values()).forEach(e-> {
            enumMap.put(e.getType(),e);
            enumMealNameMap.put(e.getName(),e);
            if (!StringUtils.isEmpty(e.getOtherName())){
                enumMealNameMap.put(e.getOtherName(),e);
            }
        });
    }

    public static BusiSampleMealTimeConfigEnum getByType(Integer type){
        if (Objects.isNull(type)){
            return null;
        }
        return enumMap.get(type);
    }

    public static BusiSampleMealTimeConfigEnum getByName(String name){
        if (StringUtils.isEmpty(name)){
            return null;
        }
        return enumMealNameMap.get(name);
    }

    public static BusiSampleMealTimeConfigEnum getMealTime(LocalDateTime sampleTime){
        if (Objects.isNull(sampleTime)){
            return null;
        }
        LocalTime time = sampleTime.toLocalTime();
        for (BusiSampleMealTimeConfigEnum e:BusiSampleMealTimeConfigEnum.values()){
            if (e.startTime.isBefore(e.endTime) && !time.isBefore(e.startTime) && !time.isAfter(e.endTime)){
                return e;
            }
            else if (e.startTime.isAfter(e.endTime) && (!time.isBefore(e.startTime) || !time.isAfter(e.endTime))){
                return e;
            }
        }
        return null;
    }

    public static BusiSampleMealTimeConfigEnum getPrevMealTime(BusiSampleMealTimeConfigEnum mealTimeConfigEnum){
        if (Objects.isNull(mealTimeConfigEnum)){
            return null;
        }
        switch (mealTimeConfigEnum){
            case ZC1:return YX;
            case ZC2:return ZC1;
            case WUC1:return ZC2;
            case WUC2:return WUC1;
            case WANC:return WUC2;
            case YX:return WANC;
        }
        return null;
    }
}
